# -*- coding: utf-8 -*-
# Copyright 2017-2019 ControlScan, Inc.
#
# This file is part of Cyphon Engine.
#
# Cyphon Engine is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Cyphon Engine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Cyphon Engine. If not, see <http://www.gnu.org/licenses/>.
#
# Generated by Django 1.11.2 on 2017-12-12 19:10
from __future__ import unicode_literals

from django.db import migrations


def populate_names(apps, schema_editor):
    """Populate names for Distilleries that don't have one."""
    Distillery = apps.get_model('distilleries', 'Distillery')
    for distillery in Distillery.objects.filter(name__isnull=True):
        collection = distillery.collection
        warehouse = collection.warehouse
        distillery.name = '%s.%s.%s' % (warehouse.backend, warehouse.name,
                                        collection.name)
        distillery.save()


class Migration(migrations.Migration):

    dependencies = [
        ('distilleries', '0002_add_name_field'),
    ]

    operations = [
        migrations.RunPython(populate_names),
    ]
